/*
** $Id: decrypt_aes_cbc.java 10 2014-08-24 13:20:16Z chupin007@gmail.com $
** $HeadURL: http://bapps.googlecode.com/svn/trunk/src/enc/src/decrypt_aes_cbc.java $
*/
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

public class decrypt_aes_cbc {

  public static byte[] hexToByte( String hexString){
      int len = hexString.length();
      byte[] ba = new byte[len / 2];
      for( int i = 0; i < len; i += 2 ) ba[i/2] = (byte) (( Character.digit( hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i+1), 16) );
      return ba;
  }

  public static void main(String args[]) throws Exception {
    Cipher dcipher;
    byte[] iv = new byte[] { 0,1,2,3,4,5,6,7,8,9,0,'a','b','c','d','e' };
    InputStream is = new FileInputStream( new File( "temp.key" ) );
    byte[] keyBytes = new byte[16];
    is.read( keyBytes, 0, 16 );
    if( args.length < 1 ) { System.exit(1); }
    String encdata = args[0];
    SecretKeySpec skey = new SecretKeySpec( keyBytes, "AES");
    AlgorithmParameterSpec paramSpec = new IvParameterSpec( iv );
    dcipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
    dcipher.init( Cipher.DECRYPT_MODE, skey, paramSpec );
    String plaintext = new String(dcipher.doFinal(hexToByte(encdata)), "UTF-8");
    System.out.println( plaintext );
  }

}
